<?php
class SiteController extends Controller{
	public function actions(){
		return array(
			'captcha'			=> array(
				'class'			=> 'CCaptchaAction',
				'backColor'		=> 0xFFFFFF,		//背景颜色
                'minLength'		=> 4,  				//最短为4位
                'maxLength'		=> 4,   			//是长为4位
                'transparent'	=> false,  			//显示为透明
                'height'		=> 30,
				'width'			=> 60,
				'padding'		=> 0
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex(){
		$this->pageTitle=Yii::app()->name;
		$this->render('index');
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError(){
		$error = Yii::app()->errorHandler->error;
		if($error){
			if(Yii::app()->request->isAjaxRequest){
				echo $error['message'];
			}else{
				$this->render('error', $error);
			}
		}
	}

	/**
	 * 用户登录
	 */
	public function actionLogin(){
		$model = new LoginForm;
		
		if(isset($_POST['LoginForm'])){
			$model->attributes = $_POST['LoginForm'];
			$return_url = '/';
			if($model->validate() && $model->login()){
				if(isset($_REQUEST['return_url'])){
					$return_url = $_REQUEST['return_url'];
				}
				$this->redirect($return_url);
			}
		}
		$this->layout = '//layouts/layout_ucenter';
		$this->render('login', array(
			'model' => $model,
			'title' => '用户登录'	
		));
	}
	
	public function actionRegist(){
		if(Yii::app()->request->isPostRequest){
			$this->_regist();
		}
		$this->layout = '//layouts/layout_ucenter';
		$this->render('regist', array(
			'title' => '用户注册'
		));
	}
	
	private function _regist(){
		$user = $_POST['User'];
		
		if(!isset($user['user_name']) || empty($user['user_name'])){
			$this->ajax_error('user_name', '用户名不能为空！');
		}
		if(strlen($user['user_name']) < 6){
			$this->ajax_error('user_name', '用户名长度必须大于5！');
		}
		
		//检查密码是否一致
		if(!isset($user['password']) || !isset($user['password_confirm'])){
			$this->ajax_error('password', '密码不正确！');
		}
		if($user['password'] != $user['password_confirm']){
			$this->ajax_error('password_confirm', '两次密码输入不一致！');
		}
		if(strlen($user['password']) < 6){
			$this->ajax_error('password', '密码长度应该大于6！');
		}
		unset($user['password_confirm']);

		if(!isset($_REQUEST['captcha'])){
			$this->ajax_error('captcha', '验证码不能为空！');
		}
		$captcha = $this->createAction('captcha');
		if(!$captcha->validate($_REQUEST['captcha'], false)){
			$this->ajax_error('captcha', '验证码错误！');
		}
		
		//检查用户名是否已经存在
		$con = array(
			'site_id' 	=> SITE_ID,
			'user_name' => $user['user_name']
		);
		if(UserModel::model()->exists_attrs($con)){
			$this->ajax_error('user_name', '用户名已经存在！');
		}
		
		$user['_id'] 			= CommonHelper::gen_uuid();
		$user['password'] 		= md5($user['password'] . Yii::app()->params['fingerprint']);
		$user['create_time'] 	= time();
		$user['site_id'] 		= SITE_ID;
		$user['status'] 		= UserModel::STATUS_ENABLE;
		UserModel::model()->insert($user);
		
		//用户账户
		$user_account = new User();
		$user_account->setPrimaryKey($user['_id']);
		$user_account->attributes = array(
			'coin' 			=> 0,
			'total_coin' 	=> 0,
		);
		$user_account->insert();
		
		//发送通知
		NoticeServer::notice('regist', array(
			'user' => $user,
		));
		
		//注册成功，顺便登录
		$identity = new UserIdentity($user['user_name'], $user['password']);
		$duration = 0;//$this->rememberMe ? 3600*24*30 : 0; // 30 days
		Yii::app()->user->login($identity, $duration);
		
		$this->ajax_info('保存成功！');
	}

	/**
	 * 用户登出
	 */
	public function actionLogout(){
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
	
	public function actionUserInfo(){
		$data = array();
		if(Yii::app()->user->isGuest){
			$data['isGuest'] = true;
		}else{
			$data['isGuest'] = false;
			$data['name'] = Yii::app()->user->name;
			$data['roles'] = Yii::app()->user->roles;
		}
		$this->renderPartial('user_info', array('user_info' => $data));
	}
}